package com.mytest.utils;

import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class JsonPahtTest {


    public static void main(String[] args) {

        String queryString= "[ \n" +
                "  { \n" +
                "    \"eventId\": \"\", \n" +
                "    \"data\": { \n" +
                "      \"packages\": [ \n" +
                "        { \n" +
                "          \"algorithms\": [ \n" +
                "            { \n" +
                "              \"algorithmName\": \"娱乐场所违法经营监控\", \n" +
                "              \"algorithmCode\": \"ZZQJ-1717110001\" \n" +
                "            } \n" +
                "          ], \n" +
                "          \"application\": { \n" +
                "            \"productCode\": \"1.0\", \n" +
                "            \"appCode\": \"000000\", \n" +
                "            \"productName\": \"1.0\" \n" +
                "          }, \n" +
                "          \"devices\": [ \n" +
                "            { \n" +
                "              \"devName\": \"111.22.122.63\", \n" +
                "              \"deviceCode\": \"11010000831187000002\" \n" +
                "            }, \n" +
                "            { \n" +
                "              \"deviceCode\": \"11010000831187000001\", \n" +
                "              \"deviceName\": \"10.6.3.130\" \n" +
                "            }, \n" +
                "            { \n" +
                "              \"devName\": \"00000000001311000004\", \n" +
                "              \"deviceCode\": \"11010000831327000001\" \n" +
                "            } \n" +
                "          ], \n" +
                "          \"sensorDevice\": [ \n" +
                "            { \n" +
                "              \"deviceCode\": \"11010912456875249\", \n" +
                "              \"deviceName\": \"test2222222\" \n" +
                "            } \n" +
                "          ], \n" +
                "          \"packageCode\": \"000000\", \n" +
                "          \"packageName\": \"通用版本\" \n" +
                "        } \n" +
                "      ], \n" +
                "      \"customer\": { \n" +
                "        \"customerCode\": \"TY0000001\" \n" +
                "      } \n" +
                "    } \n" +
                "  } \n" +
                "]";
        DocumentContext documentContext = JsonPath.parse(queryString);
// 如果是数组形式
        List<String> paramValues = documentContext.read("$..algorithms[*].algorithmCode", List.class);
// 如果是单个
//        String paramValue = documentContext.read("$."+paramName);
        log.info("paramValues:{}",paramValues);

        List<String> values = documentContext.read("$.[*].data.packages[*].algorithms[*].algorithmName", List.class);
        log.info("values:{}",values);
    }
}
